from fastapi import FastAPI
from fastapi.responses import FileResponse
from pydantic import BaseModel
import api

app = FastAPI()
api_manager = api.ApiManager()
api_manager.new(1)
# api_manager.api[1].load_all()
# api_manager.api[1].index_all()
# api_manager.api[1].save_all()

@app.get('/serveroot/{path:path}')
def root(path):
    return FileResponse(api.os.path.join(api_manager.get("v1").root_dir, path))

class AdminMetaData(BaseModel):
    token: str
    data: str

@app.post('/admin')
def admin(data: AdminMetaData):
    return api_manager.api[1].admin(data.token, data.data)

@app.get('/timestamp')
def timestamp():
    return api_manager.api[1].timestamp()

# @app.get('/{version}/packages/{packname}')
# def package(version: str, packname: str):
#     return api_manager.get(version).get_package(packname)

# class PackjsonModel(BaseModel):
#     packjson: str

# @app.post('/{version}/packages')
# def upload_pack(version: str, packjson: PackjsonModel):
#     return api_manager.get(version).upload_package(packjson.packjson)

# @app.post('/{version}/mods')
# def mod(version: str, name, game_version, mod_version, traname = None, file: UploadFile = File(None)):
#     return api_manager.get(version).upload_mod(name, mod_version, game_version, traname, file)

# @app.get('/{version}/mods/{name}')
# def get_mod(version: str, name: str):
#     return api_manager.get(version).get_mods_by_name(name)

@app.get('/{version}/mods/get')
def get_mods(version: str = "v1", modname: str = None, modver: str = None, gamever: str = None):
    return api_manager.get(version).get_mods(modname, modver, gamever)

@app.get('/{version}/packages/get')
def get_packages(version: str = "v1", packname: str = None, packver: str = None, gamever: str = None):
    return api_manager.get(version).get_packages(packname, packver, gamever)

@app.get('/{version}/packages/getall')
def get_all_packages(version: str = "v1"):
    return api.jsonify(200, api_manager.get(version).package_index["all"])

@app.get('/{version}/packages/getall_name')
def get_all_packages(version: str = "v1"):
    return api.jsonify(200, list(api_manager.get(version).package_index["name"].keys()))

@app.get('/{version}/hash/{packid}/{file}')
def get_hash(version: str, packid: str, file: str):
    return api_manager.get(version).get_hash(str(packid), file)
